/*
 * JHeuristics - The Java Meta-Heuritics Library
 * Copyright (C) 2004-2005, Universidade Federal de Campina Grande. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.jheuristics.util;

import java.io.Serializable;
import java.util.BitSet;

/**
 * TODO
 *
 * @author Marcell Manfrin, marcell@ourgrid.org, Jan 9, 2006
 */
public class BitString implements Serializable {
	private BitSet bitset;
	private int length;

	/**
	 * TODO
	 *
	 * @param nbits
	 */
	public BitString(int nbits) {
	    if (0 > nbits) {
	        // TODO: ERROR MSG
	        throw new IllegalArgumentException();
	    }
		length = nbits;
		bitset = new BitSet(length);
	}

	/**
	 * TODO
	 *
	 * @param bitString
	 */
	public BitString(BitString bitString) {
	    if (null == bitString) {
			// TODO: ERROR MSG
			throw new NullPointerException();
	    }
	    length = bitString.length;
	    bitset = (BitSet) bitString.bitset.clone();
	}
	
	/**
	 * TODO
	 *
	 * @param nbits
	 * @param bitset
	 */
	private BitString(int nbits, BitSet bitset) {
		length = nbits;
		this.bitset = bitset;
	}

	/**
	 * TODO
	 *
	 * @param nbits
	 * @param random
	 */
	public BitString(int nbits, RandomGenerator random) {
	    if (0 > nbits) {
	        // TODO: ERROR MSG
	        throw new IllegalArgumentException();
	    }
	    if (null == random) {
			// TODO: ERROR MSG
			throw new NullPointerException();
	    }
		length = nbits;
		bitset = new BitSet(length);
		for (int i = 0; i < length; i++) {
			bitset.set(i, random.nextBoolean());
		}
	}

	/**
	 * TODO
	 *
	 * @param index
	 * @return
	 */
	public boolean get(int index) {
		if (index >= length || index < 0) {
			// TODO: ERROR MSG
			throw new IndexOutOfBoundsException();
		}
		return bitset.get(index);
	}

	/**
	 * TODO
	 *
	 * @param index
	 */
	public void flip(int index) {
		if (index >= length || index < 0) {
			// TODO: ERROR MSG
			throw new IndexOutOfBoundsException();
		}
		bitset.flip(index);
	}

	/**
	 * TODO
	 *
	 * @param index
	 * @param value
	 */
	public void set(final int index, boolean value) {
		if (index >= length || index < 0) {
			// TODO: ERROR MSG
			throw new IndexOutOfBoundsException();
		}
		bitset.set(index, value);
	}
	
	/**
	 * TODO
	 *
	 * @param index
	 * @return
	 */
	public BitString[] split(int index) {
		if (index >= length || index < 0) {
			// TODO: ERROR MSG
			throw new IndexOutOfBoundsException();
		}
		BitString bs1 = new BitString(index, bitset.get(0, index));
		BitString bs2 = new BitString(length - index, bitset.get(index, length));
		return new BitString[] {bs1, bs2};
	}

	/**
	 * TODO
	 *
	 * @param bs1
	 */
	public void concat(BitString bitString) {
	    if (null == bitString) {
			// TODO: ERROR MSG
			throw new NullPointerException();
	    }
		for (int i = 0; i < length + bitString.length; i++) {
			bitset.set(length + i, bitString.bitset.get(i));
		}
		this.length += bitString.length;
	}

	/**
	 * TODO
	 *
	 * @param value
	 * @return
	 */
	public int countBits(boolean value) {
		if (value) {
			return bitset.cardinality();
		} else {
			return length - bitset.cardinality();
		}
	}

	/**
	 * TODO
	 *
	 * @return
	 */
	public int length() {
		return length;
	}

	/**
	 * TODO
	 *
	 * @return
	 */
	public int size() {
		return bitset.size();
	}

	/* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object that) {
        if (this == that) 
            return true;
        if (that == null || that.getClass() != this.getClass()) 
            return false;
        BitString other = (BitString) that;
        if (length != other.length) {
        	return false;
        }
        for (int i = 0; i < length; i++) {
        	if (bitset.get(i) != other.bitset.get(i)) {
        		return false;
        	}
        }
        return true;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {
        return (23*37 + length)*37+bitset.hashCode();
    }

    /* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return bitset.toString();
	}

}
